<template>
  <div>
    <div v-for="(i,index) in gwclist" :key="index">
    <van-swipe-cell>
      <van-card
        :num=i.num
        :price=i.price
        :title=i.produce
        :thumb="i.url">
</van-card>
<!-- 右侧删除按钮 -->
<template #right>
    <van-button square text="删除" type="danger" @click="aaaa(i.id)" class="delete-button" />
  </template>
    </van-swipe-cell>
      <hr>
    </div> <!--   end of 商品列表 -->
    <div style="height:95px"></div>
    <van-submit-bar v-show="this.gwclist != []" button-text="提交订单" @submit="del2" style="margin-bottom:60px"/>
    
  </div>
</template>

<script>
import order from '@/api/order'
import gwc from '@/api/gwc'
import { Toast } from 'vant';
export default {
  data() {
    return {
      price:0,
      userid:'',
      gwclist:[
        
      ]
    };
  },
  created(){
    if (this.$route.params && this.$route.params.id){
          this.userid= this.$route.params.id
          console.log(this.userid)
          gwc.getOne(this.userid)
          .then(res=>{
            for(var i=0;i<res.data.length;i++){
              this.price = this.price+ res.data[i].price
              let list = {
                id:res.data[i].id,
                price:res.data[i].price,
                num:res.data[i].num,
                produce:res.data[i].produce,
                url:res.data[i].url
              }
              this.gwclist.push(list)
            }
          })
      }
  },
  methods:{
    del2(){//全部购买，根据用户id删除，且加入订单表
    for(var i =0;i<this.gwclist.length;i++){
      let o ={
        uid:this.userid,
        produce:this.gwclist[i].produce,
        url:this.gwclist[i].url
      }
      order.addOder(o)
    }
    gwc.del(this.userid)
    this.gwclist=[]
    Toast("提交成功")
    },
    aaaa(i){//单个删除，根据商品id删除
      Toast(i)
      gwc.delbyid(i),
      // this.refresh()
      this.$router.push({ path: '/GWC/'+this.userid })
    },
    // refresh(){
    //   this.gwclist=[]
    //   gwc.getOne(this.userid)
    //       .then(res=>{
    //         for(var i=0;i<res.data.length;i++){
    //           this.price = this.price+ res.data[i].price
    //           let list = {
    //             id:res.data[i].id,
    //             price:res.data[i].price,
    //             num:res.data[i].num,
    //             produce:res.data[i].produce,
    //             url:res.data[i].url
    //           }
    //           this.gwclist.push(list)
    //       }
    //   })
    // }
  }
};
</script>

<style>
  .delete-button {
    height: 100%;
  }
</style>
